/*
 *  game_ui.cpp
 *  BioBreak
 *
 *  Created by terry franguiadakis on 2/22/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
// -----------------------------------------------------------------------------

#include "core.h"
#include "go.h"
#include "uimgr.h"
#include "ui_elements.h"
#include <vector>
#include "texture.h"
#include "engine.h"
#include "touchmgr.h"
#include "timermgr.h"
#include "world.h"
#include "player.h"
#include "bbmath.h"

using namespace std;

/*--------------------------------------------------------------------------*/

CUIMgr::CUIMgr() : super("UIMgr")
{
    m_textureCounter = 0;
}

void CUIMgr::OnInit()
{
	BBBackElement* toolbar = new BBBackElement();
	toolbar->SetTextureUI(g_texturemgr.CreateTexture("toolbar_bg.png"));
    m_uielements.push_back(toolbar);
	
	//create the pause button
	m_pause = new BBUIButton(Vector2f(280.0f, 52.0f), 42.0, EClass_Invaild, BBUIButton::EButtonFunction_Pause);
	m_pause->SetTextureUI(g_texturemgr.CreateTexture("pause_icon.png"));
	
	m_play = new BBUIButton(Vector2f(280.0f, 52.0f), 42.0, EClass_Invaild, BBUIButton::EButtonFunction_Play);
	m_play->SetTextureUI(g_texturemgr.CreateTexture("play_icon.png"));
	
    const EClass classes[] = {
           EClass_ExpansionNode, EClass_HealStation, EClass_BasicWeapon, EClass_MiningStation
    };
        
    for  (int i = 0 ; i < DIM(classes) ; i++ )
    {
        float xpos = (i * 85) + 10;
        BBUIElement *tiuie = new BBUIButton(Vector2f(xpos+22,460.0), 50.0, classes[i]);              
        m_uielements.push_back(tiuie);  
    }

	m_controllingElement = 0;
    
	m_textureCounter = g_texturemgr.CreateTextureCounter();
	
	m_globalTimer = new CUITimer(Vector2f(120.0f, 52.0f));
}

/*--------------------------------------------------------------------------*/

void CUIMgr::UIRender()
{
	if (!g_engine.IsInGameState())
	{
		return;
	}
	
	m_globalTimer->Render();
	
	if (m_pause->IsVisible())
	{
		m_pause->Render();
	}
	
	if (m_play->IsVisible())
	{
		m_play->Render();
	}
	
    for (int i = 0 ; i < m_uielements.size(); i++)
	{
		if (m_uielements[i]->IsVisible())
		{
			m_uielements[i]->Render();
		}
    }
	
    if (m_controllingElement != NULL)
    {
        if (m_controllingElement->IsPlaced())
        {
            m_controllingElement->ClearPlaced();
            m_controllingElement = NULL;
        }
    }
    
    // if world is loaded show stuff.
    if (g_world.IsLoaded())
    {
        int resource = g_player.GetMineralCount();
        char msg[255];
        sprintf(msg,"%d", resource);
        m_textureCounter->SetString(msg);
        g_texturemgr.EnableTextures(true);

        Transform2D::glPushMatrix();
        Transform2D::glTranslatef(250,75,0);
        m_textureCounter->SetInterval(15, 30);
        m_textureCounter->Apply();
        m_textureCounter->Draw(Vector2f(0,0), Color4f(0,0,0,1));
        Transform2D::glPopMatrix();
        g_texturemgr.EnableTextures(false);
        
    }
    
}

void CUIMgr::OnPreWorldUpdate()
{
	m_globalTimer->Update();
	
	m_play->SetIsVisible(g_timemgr.IsPaused());
	m_pause->SetIsVisible(g_timemgr.IsRunning());

	if (g_touchmgr.HasContact())
	{
		if (g_touchmgr.HasTouchEvent(ETouchType_SingleTouchStart, false))
		{
			Vector2f screenTouchPos = g_touchmgr.GetTouchEventScreenPos(ETouchType_SingleTouchStart);
			
			//check the play
			if (m_play->IsVisible() && m_play->IsPointContained(screenTouchPos))
			{
				g_touchmgr.ConsumeTouchEvent(ETouchType_SingleTouchStart);
				
				//play game
				g_timemgr.Run();
			}
			else if (m_pause->IsVisible() && m_pause->IsPointContained(screenTouchPos))
			{
				g_touchmgr.ConsumeTouchEvent(ETouchType_SingleTouchStart);
				
				//pause game
				g_timemgr.Pause();
			}
		}
		
		if (m_controllingElement == NULL) 
		{
			if (g_touchmgr.HasTouchEvent(ETouchType_SingleTouchStart, false))
			{
				for (int i = 0 ; i < m_uielements.size() ; i++ )
				{
					Vector2f screenTouchPos = g_touchmgr.GetTouchEventScreenPos(ETouchType_SingleTouchStart);
					if (m_uielements[i]->IsVisible() && m_uielements[i]->IsPointContained(screenTouchPos))
					{	
						g_touchmgr.ConsumeTouchEvent(ETouchType_SingleTouchStart);
						
						if (m_uielements[i]->HasAction())
						{
							m_controllingElement = static_cast<BBUIButton*>(m_uielements[i]);
							m_controllingElement->Apply(screenTouchPos);
							break;
						}
					}
				}
			}
		}
		else if (g_touchmgr.HasTouchEvent(ETouchType_SingleTouchEnd, false))
		{
			for (int i = 0 ; i < m_uielements.size() ; i++ )
			{
				Vector2f screenTouchPos = g_touchmgr.GetTouchEventScreenPos(ETouchType_SingleTouchEnd);
				if (m_uielements[i]->IsPointContained(screenTouchPos))
				{
					g_touchmgr.ConsumeTouchEvent(ETouchType_SingleTouchEnd);
					
					// if we stop touching over any ui element remove the controlling element
					m_controllingElement->Remove();
					m_controllingElement = NULL;
					break;
				}
			}
		}
	}
	
	for (int i = 0 ; i < m_uielements.size() ; i++ )
	{
		m_uielements[i]->Update();
	}
}

void CUIMgr::SetGlobalTimer(float i_timeLeft) 
{
	return m_globalTimer->SetTimer(i_timeLeft); 
}

void CUIMgr::StartGlobalTimer() 
{
	return m_globalTimer->StartTimer(); 
}			

void CUIMgr::PauseGlobalTimer() 
{
	return m_globalTimer->PauseTimer(); 
}			

void CUIMgr::StopGlobalTimer() 
{
	return m_globalTimer->StopTimer();
}			

bool CUIMgr::IsComplete() const 
{
	return m_globalTimer->IsComplete(); 
}

/*----------------------------------EOF-------------------------------------*/
